﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace SoftwareLogistics.SharepointClient.Models
{
    [DataContract]
    public class SPDataRow
    {
        public SPDataRow()
        {
            Columns = new Dictionary<string, string>();
        }

        public static SPDataRow Create(XElement element)
        {
            var row = new SPDataRow();

            foreach (var attribute in element.Attributes())
            {
                string key = attribute.Name.LocalName.Replace("ows_", "");
                key = key.Replace("_x0020_", " ");
                row.Columns.Add(key, attribute.Value);
            }

            if (!row.Columns.ContainsKey("Title"))
                row.Columns["Title"] = "[no title]";
            else if (string.IsNullOrEmpty(row.Columns["Title"]))
                row.Columns["Title"] = "[no title]";

            var title = element.Attribute(XName.Get("ows_Title"));
            if (title == null)
                row.Title = "[no title]";
            else
                row.Title = string.IsNullOrEmpty(title.Value) ? "[no title]" : title.Value;

            row.ID = element.Attribute(XName.Get("ows_ID")).Value.ToString();

            return row;
        }

        [DataMember] public string ListName { get; set; }
        [DataMember] public string ID { get; set; }
        [DataMember] public string Title { get; set; }
        [DataMember] public Dictionary<string, string> Columns { get; set; }
    }
}
